/*
 * Copyright (C) 2015 Miquel Sas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package traderlib.core.data.db;

import java.util.Comparator;
import java.util.List;
import traderlib.core.data.types.KeyPointer;
import traderlib.core.data.types.OrderKey;

/**
 * A comparator useful to compare records and sort them within a record set or list.
 *
 * @author Miquel Sas
 */
public class RecordComparator implements Comparator<Record> {

	/**
	 * The key pointers.
	 */
	private final List<KeyPointer> keyPointers;

	public RecordComparator(List<KeyPointer> keyPointers) {
		super();
		this.keyPointers = keyPointers;
	}

	/**
	 * Compares two records based on the order key.
	 *
	 * @param r1
	 * @param r2
	 * @return -1 if r1 is less than r2, 0 if equal, and 1 if  greater than.
	 */
	@Override
	public int compare(Record r1, Record r2) {
		OrderKey k1 = r1.getIndexKey(keyPointers);
		OrderKey k2 = r2.getIndexKey(keyPointers);
		return k1.compareTo(k2);
	}
}
